package lib

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/m"

	log "github.com/sirupsen/logrus"
)

// 加载委托活动，只能加载一个，或者没有
func GetCurrentEntrustActiv() (activ data.EntrustActiv, err error) {
	nowTime := utils.TNow().Unix()

	// 获取委托任务
	tplLst, ok := data.GetAllDatas("EntrustActivs")
	if !ok {
		err = errors.NewErrcode(data.Errcode.NotFound, "没有找到活动配表")
		return
	}

	for _, item := range tplLst {
		itemT := item.(data.EntrustActiv)
		beginTime, _ := utils.LayoutToTime(itemT.BeginTime)
		endTime, _ := utils.LayoutToTime(itemT.EndTime)
		if beginTime.Unix() <= nowTime && nowTime <= endTime.Unix() {
			return itemT, nil
		}
	}
	err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到激活活动配置,ActActivities")
	return
}

// 获取委托活动信息
func GetEAActiv() (r *m.EAActiv) {
	r = &m.EAActiv{}
	r.Load(int64(1))
	return
}

func GetEntrustActiv(u *m.User) (r *m.EAGamer) {
	r = m.GetEAGamer(u.Uid.Hex())
	return
}

// 委托任务tid，委托任务阶数stageNum
func GetEntrustReward(from string, u *m.User, tid int64, stageNum int64, PSend func(interface{}) error) (uProp *m.UpdateProp, resGoodsList []*m.Goods, boxResList []*m.BoxRes, err error) {
	eag := m.GetEAGamer(u.Uid.Hex())

	ea := eag.EntrTask

	if tid != ea.Tid {
		err = errors.NewErrcode(data.Errcode.ServerErr, "委托任务tid不对")
		return
	}
	length := len(ea.StageLst)
	if stageNum < 0 || int64(length) <= stageNum {
		err = errors.NewErrcode(data.Errcode.ServerErr, "委托任务索引越界")
		return
	}

	s := ea.StageLst[stageNum]
	if s.State != 1 {
		err = errors.NewErrcode(data.Errcode.ServerErr, "委托任务未完成，或者奖励已经领取。")
		return
	}

	uProp, resGoodsList, boxResList, err = GiveGoodsByKv(from, u, m.BaseKV{Key: s.GoodTid, Val: s.GoodNum}, PSend)
	if err != nil {
		return
	}

	// 领取奖励成功，修改状态。
	s.State = 2
	eag.Update(eag)

	return
}

func GetEntrustTask(tid int64) (r data.EntrustTask, err error) {
	tpl, ok := data.Get("EntrustTasks", uint64(tid))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "读取EntrustTasks配表失败")
		return
	}
	r = tpl.(data.EntrustTask)
	return
}

func CalcHonor(u *m.User, typeEnum int64, actionEnum int64, val int64) {
	activ := m.GetEAActiv(1)
	if activ.ActivId == 0 { // 没有活动开始
		return
	}

	eag := m.GetEAGamer(u.Uid.Hex())
	et := eag.EntrTask
	if et == nil || et.TypeNum != typeEnum || et.ActionEnum != actionEnum {
		return
	}
	honor := int64(0)
	switch typeEnum {
	case data.EntrType.Guard:
		honor = GetKillMonster(val)
	case data.EntrType.Dwarf:
		honor = GetOpenBlock(val)
	case data.EntrType.Orc:
		honor = val / et.UnitNum * et.IntegralUnit
	case data.EntrType.Goblin:
		honor = val / et.UnitNum * et.IntegralUnit
	default:
		log.WithFields(log.Fields{
			"method":     "CalcHonor",
			"uid":        u.Uid.Hex(),
			"name":       u.Name,
			"typeEnum":   typeEnum,
			"actionEnum": actionEnum,
		}).Error("委托没有此委托类型")
	}

	if honor <= 0 || len(eag.HonorLst) == 0 {
		return
	}

	eag.Honor += honor
	eag.HonorLst[eag.Issue] += honor

	honorT := eag.HonorLst[eag.Issue]
	for _, item := range et.StageLst {
		if honorT < item.StageVal {
			break
		}
		// 当前期荣誉达到值，修改状态。
		if item.State == 0 {
			item.State = 1
		}
	}
	eag.Update(eag)

	// 添加，进行排名
	AddHonor(u, honor)
}

func GetKillMonster(tid int64) (r int64) {
	tpl, ok := data.Get("KillMonsters", uint64(tid))
	if !ok {
		log.WithFields(log.Fields{
			"method": "GetKillMonster",
			"tid":    tid,
		}).Error("获取配表KillMonsters失败")
		return
	}
	tplT := tpl.(data.KillMonster)
	return int64(tplT.Integral)
}

func GetOpenBlock(tid int64) (r int64) {
	// 章节=关卡/8+1
	tid = tid/8 + 1
	tpl, ok := data.Get("OpenBlocks", uint64(tid))
	if !ok {
		log.WithFields(log.Fields{
			"method": "GetOpenBlock",
			"tid":    tid,
		}).Error("获取配表OpenBlocks失败")
		return
	}
	tplT := tpl.(data.OpenBlock)
	return int64(tplT.Integral)
}

// 检测委托任务有完成
func EntrustCheck(u *m.User) (r int64) {
	eag := m.GetEAGamer(u.Uid.Hex())
	et := eag.EntrTask
	if et == nil {
		return
	}
	for _, item := range et.StageLst {
		if item.State == 1 {
			r += 1
		}
	}
	return
}
